<?php
namespace app\common\model;
use app\common\traits\Models;
use think\Model;
use think\Db;
use think\facade\{
    App,Request,Config
};
class User extends Model
{
    use Models;
    protected $readonly = [];
    protected $auto     = [];
    protected $insert   = ['reg_ip'];
    protected $update   = ['last_login_time','last_login_ip'];
/*数据完成=================================================================================*/
    //设置Ip
    protected function setRegIpAttr()
    {
        return Request::ip(1);
    }
    //设置最后登录id
    protected function setLastLoginIpAttr()
    {
        return Request::ip(1);
    }
    //设置最后登录时间
    protected function setLastLoginTimeAttr()
    {
        return Request::time();
    }

//获取器====================================================================================//
    public function getCreateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getUpdateTimeAttr($value)
    {
        return com_time_format($value);
    }
    public function getSexAttr($value)
    {
        return c('type.sex',$value);
    }
    public function getStatusAttr($value)
    {
        return c('type.status',$value);
    }

/*模型关联=================================================================================*/
    public function userBank()
    {
        return $this->hasMany('userBank','user_id','id');
    }
    /*关联模型=================================================================================*/
   	public function userAddress()
    {
        return $this->hasMany('user','cat_id','id');
    }
 //=================================================================================
    /**
     * 登录
     * @param  [type] $username [description]
     * @param  [type] $type     [description]
     * @return [type]           [description]
     */
  	public function login($username, $type=0){
        /* 获取用户数据 */
        $user = $this::get(function ($query) use ($username, $type) {
            switch ($type) {
                case '1':
                    $field = "wx_openid";
                    break;
                default:
                    $field = "phone";
                    break;
            }
            $query->where([[$field,'=', $username]])->field('id,status');
        });
        if (empty($user) || (int)$user->status !== 0) {
            $this->error = '用户不存在或被禁用';
            return false;
        }
        /* 更新用户登录token */
        $token = com_token($user->id);
        $user->token = $token;
        $user->save();
        return $token;
  	}

    /**
     * 获取用户信息
     * @param  string $token [登录token]
     * @return [type]        [description]
     */
    public function getInfoBytoken($token=''){
        $rs = $this::get(function ($query) use ($token) {
            $query->where([['token','=', $token]]);
        });
        if($rs)
            return $rs->toArray();
        return [];
    }

    /**
     * 用户修改密码
     * @param  [type] $phone [description]
     * @param  [type] $pwd   [description]
     * @return [type]        [description]
     */
    public function editpwd($phone, $pwd){
        $rs = $this::where("phone",$phone)->find();
        if(!$rs){
            $this->error = "用户不存在";
            return false;
        }
        $rs->password = com_password($pwd);
        $rs->token = "";
        $rs->save();
        return true;
    }
}
